public static bool IsPrime(int number)
{
if (number <= 1) return false;
if (number == 2) return true;
if (number % 2 == 0) return false;
var boundary = (int)Math.Floor(Math.Sqrt(number));
for (int i = 3; i <= boundary; i+=2)
if (number % i == 0)
return false;
return true;
}
Prime Number in C# -
using System;
public class Program
{
static void Main(string[] args)
{
var results = GenerateSieve(1000);
var i=0;
foreach (var item in results)
{
if(item) Console.Write(i + " ");
i++;
}
}
static bool[] GenerateSieve(int num)
{
// Creating an array indicating whether numbers are prime.
bool[] isPrime = new bool[num + 1];
for (int i = 2; i <= num; i++) isPrime[i] = true;
// Removing out multiples.
for (int i = 2; i <= num; i++)
{
// Check if i is prime.
if (isPrime[i])
{
// Eliminate multiples of i.
for (int j = i * 2; j <= num; j += i)
isPrime[j] = false;
}
}
return isPrime;
}
}